/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 活跃奖励模块
	/// module : activity
	/// </summary>
	public interface CNetActivity : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction signResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<SignResultData>)wrapper;
                SC_signResult(session, ref _wrapper.message);
                MessageWrapperPool<SignResultData>.Put(_wrapper);
            };
            MessageFunction notifySign_Delegate = (session, wrapper) =>
            {
            var _wrapper = (MessageWrapper<NotifySignData>)wrapper; 
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.activity_notifySign, "Recv->" + _wrapper.message.ToString());
#endif
                SC_notifySign(session, _wrapper.message.signInfo);
                MessageWrapperPool<NotifySignData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.activity_signResult, (session, package) => {
                	var result = MessageWrapperPool<SignResultData>.Get();
                	result.Init(session, signResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.activity_notifySign, (session, package) => {
                	var result = MessageWrapperPool<NotifySignData>.Get();
                	result.Init(session, notifySign_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
		public void SC_signResult(ISession session, ref SignResultData message) 
		{
            _SC_signResult(session, ref message);
		}
		
		public void _SC_signResult(ISession session, ref SignResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.activity_signResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.activity_signResult, message.code);
			MsgAsyncAwaiter<SignResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        ///  推送签到
        /// </summary>
		protected void SC_notifySign(ISession session, int signInfo);
#endregion
#region Server-side Interface
		/// <summary>
        ///  签到
        /// </summary>
    	public UniTask<SignResultData> CS_sign()
        {
            return _CS_sign();
        }
        
        public UniTask<SignResultData> _CS_sign()
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(SignResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.activity_sign, "Send->"+"Activity.CS_sign { "
				+" }");
#endif
        	UniTask<SignResultData> task = MsgAsyncAwaiter<SignResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_sign());
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.activity_signResult);
			return task;
        }
		
		public static SendPackage Pack_for_sign() 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.activity_sign;
			si_p.StartWrite();
			return si_p;
		}
#endregion
#region Messages
        public struct SignResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public int signInfo;
	        public List<ItemShowPOD> shows;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                signInfo = _in.HasRemaining() ? _in.Read_int() : default;
                shows = _in.HasRemaining() ? _in.Read_list_pod<ItemShowPOD>(ItemShowPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.activity_signResult;}
            
            public override string ToString()
            {
                return "Activity.signResult"+ ", code="+ code+ ", signInfo="+ signInfo+ ", shows="+ shows.ToString<ItemShowPOD>();
            }
        }
        public struct NotifySignData : IReceiveMessage {
	        public int signInfo;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                signInfo = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.activity_notifySign;}
            
            public override string ToString()
            {
                return "Activity.notifySign"+ ", signInfo="+ signInfo;
            }
        }
    }
#endregion
}